<template>
  <div>
    <div style="width: 50% margin-top:50px">
      <div id="bar" style="width: 100%; height: 400px"></div>
    </div>
    <div style="width: 50% margin-top:50px">
      <div id="pie" style="width: 100%; height: 400px"></div>
    </div>
  </div>
</template>
<script>
import * as echarts from "echarts";
export default {
  data() {
    return {
      data: [],
    };
  },
  mounted() {
    // this.initBar();
    this.initEcharts();
  },
  methods: {
    initEcharts() {
      this.$axios
        .get("/data/TUFbar")
        .then((res) => {
          const data = res.data.data;
          const proName = data.map((item) => item.proName);
          const proNum = data.map((item) => item.proNum);

          const chartData = data.map((item) => ({
            name: item.proName,
            value: item.proNum,
          }));
          this.initBar(proName, proNum);
          this.initpie(chartData);
        })
        .catch((err) => {});
    },
    initBar(proName, proNum) {
      let chartDom = document.getElementById("bar");
      let myChart = echarts.init(chartDom);
      let option;

      option = {
        title: {
          text: "次品库存表",
          left: "center",
        },
        xAxis: {
          type: "category",
          data: proName,
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: proNum,
            type: "bar",
             itemStyle: {
            color: '#73c0de'
          }
          },
        ],
      };

      option && myChart.setOption(option);
    },
    initpie(chartData) {
      let chartDom = document.getElementById("pie");
      let myChart = echarts.init(chartDom);
      let option;

      option = {
        title: {
          text: "次品库存表",
          subtext: "按比例分配",
          left: "center",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
           
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: "50%",
            data:chartData ,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };

      option && myChart.setOption(option);
    },
  },
};
</script>
<style scoped>
</style>